-
Notifications
You must be signed in to change notification settings - Fork 664
Refactor RegexPatterns to use properties instead of methods for Regex #4748
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Refactors regex patterns to utilize `GeneratedRegexAttribute` caching for improved performance and reduced memory allocation.
unifies regex definitions into a central descriptor list. adds a default timeout to regex construction for safety.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This pull request refactors the RegexPatterns class to expose regular expressions as properties instead of methods, improving API consistency and simplifying usage across the codebase. The implementation uses conditional compilation to support both .NET 9+ (with native property support for GeneratedRegex) and earlier .NET versions (using property wrappers around generated methods).
- Converted all
GeneratedRegexmethods to properties with conditional compilation for NET9_0_OR_GREATER vs earlier frameworks - Updated all regex usage sites from method invocations (e.g.,
SwitchArgumentRegex()) to property accesses (e.g.,SwitchArgumentRegex) - Refactored the internal
Cache.KnownRegexesdictionary to use a more maintainable descriptor-based approach
Reviewed Changes
Copilot reviewed 13 out of 13 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| src/GitVersion.Core/Core/RegexPatterns.cs | Refactored all regex definitions from methods to properties with NET9+ conditional compilation; restructured cache to use descriptor pattern |
| src/GitVersion.Testing/Helpers/ParticipantSanitizer.cs | Updated SanitizeParticipantRegex() to property access |
| src/GitVersion.Output/AssemblyInfo/AssemblyInfoFileUpdater.cs | Updated assembly attribute regex dictionary initialization and method calls to use properties |
| src/GitVersion.MsBuild/Helpers/AssemblyInfoFileHelper.cs | Updated attribute and trivia regex switch expressions to use properties |
| src/GitVersion.Core/VersionCalculation/IncrementStrategyFinder.cs | Updated version bump message regex assignments to use properties |
| src/GitVersion.Core/SemVer/SemanticVersionPreReleaseTag.cs | Updated ParsePreReleaseTagRegex() to property access |
| src/GitVersion.Core/SemVer/SemanticVersionBuildMetaData.cs | Updated parse and format metadata regex calls to use properties |
| src/GitVersion.Core/SemVer/SemanticVersion.cs | Updated parse strict and loose regex calls to use properties |
| src/GitVersion.Core/MergeMessage.cs | Updated merge message format regex array initialization to use properties |
| src/GitVersion.Core/Logging/Log.cs | Updated ObscurePasswordRegex() to property access |
| src/GitVersion.Core/Formatting/StringFormatWithExtension.cs | Updated ExpandTokensRegex() to property access |
| src/GitVersion.App/ArgumentParserExtensions.cs | Updated SwitchArgumentRegex() to property access |
| src/GitVersion.Core.Tests/Core/RegexPatternTests.cs | Updated all test assertions to use property access instead of method calls |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Replaces expression-bodied properties with property-backed fields for Regex initialization to ensure a consistent approach and avoid potential re-evaluations. Affects multiple regex patterns across the project.
moves regex cache to bottom of the class, for better organization.
refactors regex patterns to remove the nested `Common` class for easier access and cleaner code.
|
|
Thank you @arturcic for your contribution! |

This pull request refactors how regular expressions are accessed throughout the codebase, changing calls from method invocations to property accesses. This simplifies usage and improves consistency in both implementation and tests.
Regex access refactoring:
RegexPatternsfrom method calls (e.g.,SwitchArgumentRegex()) to property accesses (e.g.,SwitchArgumentRegex) insrc/GitVersion.App/ArgumentParserExtensions.csandsrc/GitVersion.Core.Tests/Core/RegexPatternTests.cs. [1] [2]Test improvements:
RegexPatterns.Common,RegexPatterns.Configuration,RegexPatterns.MergeMessage,RegexPatterns.Output, andRegexPatterns.VersionCalculationwithinRegexPatternTests.cs, improving consistency and readability. [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] [14] [15] [16] [17] [18] [19] [20] [21] [22] [23] [24] [25]